$('#TX-create-album').bind('click',function(){
	$('#prompt').css("display","none");
	$('.popover-mask').show();
	$('.popover-mask').height($(document).height());
	$('.popover').slideDown(200);
	$(".imgareaselect-selection").parent("div").css("display","block");
	$(".imgareaselect-outer").css("display","block");
	
	$(".select_type_tr").show();
	$('#title').removeAttr('disabled');
	$('.upload_btn').show();
	$('#preview1').hide();
	$('#small100_img,#small50_img,#small30_img').attr('src','/assets/src/images/photo.png');
	$("#localImag").imgAreaSelect({
		remove:true
	});
	//清空
	$('#albumid').val('');
	$('#title').val('');
	$('#memo').val('');
	$('.select_type').val(1);
	
	$(".reset_img").show();
});

//重新选择图片
$(".reset_img").bind('click',function(){
	$(".reset_img").hide();
	$('.upload_btn').show();
	$('#preview1').hide();
	$('#small100_img,#small50_img,#small30_img').attr('src','/assets/src/images/photo.png');
	$("#localImag").imgAreaSelect({
		remove:true
	});
});

$('#TX-win-close').bind('click',function(){
	$('#prompt').css("display","none");
	$('.popover-mask').hide();
	$('.popover').slideUp(200);
	$(".imgareaselect-selection").parent("div").css("display","none");
	$(".imgareaselect-outer").css("display","none");
	
	$('.upload_btn').show();
	$('#preview1').hide();
	$('#small100_img,#small50_img,#small30_img').attr('src','/assets/src/images/photo.png');
	$("#localImag").imgAreaSelect({
		remove:true
	});
});

//验证新增/修改相册
$(".commit_album").bind('click',function(){
	var id = $("#albumid").val();
	var title = $("#title").val();
	var memo = $("#memo").val();
	if($.trim(title) == '')
	{
		$("#prompt").html('相册名不能为空');
        $('#prompt').css("display","block");
        return false;
	}
	if($.trim(memo) == '')
	{
		$("#prompt").html('相册描述不能为空');
        $('#prompt').css("display","block");
        return false;
	}
	if(id == '')
	{
		if($("#cover").val() == '')
		{
			$("#prompt").html('请选择相册封面');
	        $('#prompt').css("display","block");
	        return false;
		}
	}
});


$('#TX-create').bind('click',function(){
	$.ajax( {
	    url:'/m/works/addalbum',
	    data:{
                agid : $('#agid').val(),
                id : $('#albumid').val(), 
	            title : $('#title').val(),
	            memo : $('#memo').val(),
                type : $('.select_type').val()
	    },
	    type:'post',
	    cache:false,
	    dataType:'json',
	    success:function(res) {
	        if(res.code ==200 ){
	            $('#TX-win-close').click();
	            window.location.reload();
	            $('#X-tit-'+$('#albumid').val()).html($('#title').val());
	        }else{
	            var msg = '';
            	$.each(res.data.error_messages,function(n,value) {
	            	msg +=value+'\n';
	            });
	            if(msg!='')
	            	alert(msg);
	        }
	     }
	});
});

$('.XT-album').bind('click',function(){
	$(".select_type_tr").show();
	$('#title').removeAttr('disabled');
	$('#albumid').val($(this).attr('_val'));
	$.ajax( {
	    url:'/m/works/album',
	    data:{
	    		id : $('#albumid').val()
	    },
	    type:'get',
	    cache:false,
	    dataType:'json',
	    success:function(res) {
	        if(res.code ==200 ){
	        	if(res.data.kind == 1)
	        	{
	        		$('#title').attr('disabled','disabled');
	        		$(".select_type_tr").hide();
	        	}
	        	else
	        	{
	        		$(".select_type").find("option[value='"+res.data.type+"']").attr("selected",true);
	        	}
	            $('#title').val(res.data.title);
	            $('#memo').val(res.data.memo);
	        }else{
	            var msg = '';
            	$.each(res.data.error_messages,function(n,value) {
	            	msg +=value+'\n';
	            });
	            if(msg!='')
	            	alert(msg);
	        }
	     }
	});

	$('#X-title').html('修改相册');
	$('.popover-mask').show();
	$('.popover-mask').height($(document).height());
	$('.popover').slideDown(200);
	
	$("#pic_label").text($(this).attr("type-data"));
	$(".imgareaselect-selection").parent("div").css("display","block");
	$(".imgareaselect-outer").css("display","block");
});

$('.XT-del-album').bind('click',function(){
	//var obj = $(this).parent().parent().parent().parent();
	var obj = $(this).parents('li');
	
	if(!confirm("确定要删除相册吗?")){return}
	$.ajax( {
	    url:'/m/works/delalbum',
	    data:{
                id : $(this).attr('_val'),
                userid : $('input[name="userid"]').val()
	    },
	    type:'post',
	    cache:false,
	    dataType:'json',
	    success:function(res) {
	        if(res.code ==200 ){
	            obj.remove();
	        }else{
	            var msg = '';
            	$.each(res.data.error_messages,function(n,value) {
	            	msg +=value+'\n';
	            });
	            if(msg!='')
	            	alert(msg);
	        }
	     }
	});
});

//检验图片
function checkImgType(this_){
   var filepath=$(this_).val();
   var extStart=filepath.lastIndexOf(".");
   var ext=filepath.substring(extStart,filepath.length).toUpperCase();
   if(ext!=".PNG"&&ext!=".GIF"&&ext!=".JPG"){
	   alert("图片限于png,gif,jpg格式");
	   $(this_).focus();
	   $(this_).val("");
	   return false;
   }
 
    var file_size = 0;          
    file_size = this_.files[0].size;   
	var size = file_size/1024/1024;  
	if(size > 3){  
		alert("上传的文件大小不能超过3M！");  
		$(this_).focus();
		$(this_).val("");
		return false ;
	}
    return true;
};

//上传图片
function uploadImage(file)
{
	$(".reset_img").show();
	if(!checkImgType(file))
	{
		return false;
	}
	
//	var MAXWIDTH = $("#length_size").val();
//	var MAXHEIGHT = $("#height_size").val();
	var MAXWIDTH = 260;
	var MAXHEIGHT =256;
	var div = document.getElementById('localImag');
	var div100 = document.getElementById('small100');
	var div50 = document.getElementById('small50');
	var div30 = document.getElementById('small30');
	if (file.files && file.files[0])
	{
		div.innerHTML = '<img id=preview1>';
		var img = document.getElementById('preview1');
		img.onload = function(){
			var theImage = new Image();
			theImage.src = $("#preview1").attr("src");
			var rel_Width = theImage.width;
			var rel_Height = theImage.height;
			
			trueWidth=img.offsetWidth;
			trueHeight=img.offsetHeight;
			var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
			img.width = rect.width;
			img.height = rect.height;
		//img.style.marginLeft = rect.left+'px';
		//	img.style.marginTop = rect.top+'px';
			
			var wRate=trueWidth/rect.width;
			var hRate=trueHeight/rect.height;
			img.hRate=hRate;
			img.hRate=hRate;
			
			$('#localImag').css('margin-left',0);
//			$('#localImag').css('margin-top',rect.top);
			$('#id_widthPro').val(wRate);
			$('#id_heightPro').val(hRate);
			
			var y2 = 0;
			var x2 = 0;
			if(img.width > img.height*MAXWIDTH/MAXHEIGHT)
			{
				y2 = img.height;
				x2 = img.height*MAXWIDTH/MAXHEIGHT;
			}
			else
			{
				x2 = img.width;
				y2 = img.width*MAXHEIGHT/MAXWIDTH;
			}
//			console.info(MAXWIDTH+"X"+MAXHEIGHT+"   :   "+x2+"X"+y2+"  ：   "+img.width+"X"+img.height);
			$("#rel_width_size").val(rel_Width);
			$("#rel_height_size").val(rel_Height);
			$("#length_size").val(img.width);
			$("#height_size").val(img.height);
			
			$('#id_top').val(0);
			$('#id_left').val(0);
			$('#id_right').val(x2);
			$('#id_bottom').val(y2);
			
			$("#localImag").imgAreaSelect({
				aspectRatio:MAXWIDTH+':'+MAXHEIGHT,
				x1:0,
				y1:0,
//				x2: img.width,
//				y2: img.height,
				x2: x2,
				y2: y2,
				wRate: img.wRate,
				hRate: img.hRate,
				handles:true,
				onSelectEnd:function(img, selection) {
							$('#id_top').val(selection.y1);
							$('#id_left').val(selection.x1);
							$('#id_right').val(selection.x2);
							$('#id_bottom').val(selection.y2);
							
						},
				fadeSpeed:200,//出来效果
				onSelectChange:preview //执行选择后执行的函数
			});
			
		}
		var reader = new FileReader();
		reader.onload = function(evt){img.src = evt.target.result;$('#small100_img').attr('src',evt.target.result);$('#small50_img').attr('src',evt.target.result);$('#small30_img').attr('src',evt.target.result);}
		reader.readAsDataURL(file.files[0]);
	}
	else
	{
		
		var sFilter='filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
		file.select();
		file.blur();
		var src =document.selection.createRange().htmlText;
		div.innerHTML = '<img id=preview1>';
		var img = document.getElementById('preview1');
		img.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
		
		div100.innerHTML = '<img id=small100_img>';
		var img100 = document.getElementById('small100_img');
		img100.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
		
		div50.innerHTML = '<img id=small50_img>';
		var img50 = document.getElementById('small50_img');
		img50.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
        
		div30.innerHTML = '<img id=small30_img>';
		var img30 = document.getElementById('small30_img');
		img30.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
		
		trueWidth=img.offsetWidth;
		trueHeight=img.offsetHeight;
		var rect = clacImgZoomParam(MAXWIDTH, MAXHEIGHT, img.offsetWidth, img.offsetHeight);
		status =('rect:'+rect.top+','+rect.left+','+rect.width+','+rect.height);
		div.innerHTML = "<div id=divhead style='width:"+rect.width+"px;height:"+rect.height+"px;"+sFilter+src+"\"'></div>";
				
		$('#localImag').css('margin-left',0);
		$('#localImag').css('margin-top',rect.top);
	
		$('#id_widthPro').val(trueWidth/rect.width);
		$('#id_heightPro').val(trueHeight/rect.height);
		
		
		$('#id_top').val(0);
		$('#id_left').val(0);
		$('#id_right').val(rect.width);
		$('#id_bottom').val(rect.height);
		
		$("#localImag").imgAreaSelect({
			aspectRatio:MAXWIDTH+':'+MAXHEIGHT,
			x1:0,
			y1:0,
			x2: rect.width,
			y2: rect.height,
			handles:true,
			onSelectEnd:function(img, selection) {
						$('#id_top').val(selection.y1);
						$('#id_left').val(selection.x1);
						$('#id_right').val(selection.x2);
						$('#id_bottom').val(selection.y2);
					},
			fadeSpeed:200,//出来效果
			onSelectChange:preview //执行选择后执行的函数
		});
	}
	$('.upload_btn').hide();
	
}
